<?php
defined ( 'BASEPATH' ) or exit ( 'No direct script access allowed' );
/**
 * 返回值类
 *
 * @author LeeNux
 * @version 1.0
 *         
 */
class Callback {
	public $code = ''; // 返回码
	public $data = ''; // 返回数据
	public $msg = ''; // 返回信息
	public $backArr = array(); // 返回值数组
	public function __construct() {
		$this->backArr = array(
				'code' => $this->code, 
				'data' => $this->data, 
				'msg' => $this->msg 
		);
	}
	/**
	 * 序列化返回数据
	 *
	 * @param array $params        	
	 */
	private function _serialize() {
		$this->backArr = array(
				'code' => $this->code, 
				'data' => $this->data, 
				'msg' => $this->msg 
		);
		return true;
	}
	// ------------------------------------------------------------------------
	/**
	 * 直接根据MIME类型输出数据
	 *
	 * @param string $format        	
	 */
	public function output($mime = 'Json') {
		$this->_serialize ();
		$route = '_out' . $mime;
		$this->$route ();
		return TRUE;
	}
	// 构造json输出格式
	private function _outJson() {
		$CI = & get_instance ();
		$string = json_encode ( $this->backArr );
		$CI->output->set_content_type ( 'application/json' );
		$CI->output->set_output ( $string );
	}
	// 构造xml输出格式
	private function _outXml() {
		$CI = & get_instance ();
		$CI->load->library ( 'xml' );
		$string = $CI->xml->arrToXml ( $this->backArr );
		$CI->output->set_content_type ( 'application/xml' );
		$CI->output->set_output ( $string );
	}
	// ------------------------------------------------------------------------
	/**
	 * 数据提交
	 * 说明：通过模拟表单提交的方式向目标地址（url）提交数据
	 *
	 * @param string $url        	
	 * @param string $method
	 *        	Range : get/post
	 */
	public function submit($url, $method = 'get') {
		$CI = &get_instance ();
		$data = array(
				'url' => $url, 
				'method' => $method, 
				'code' => $this->code, 
				'msg' => $this->msg, 
				'data' => $this->data 
		);
		$CI->load->view ( 'assists/callbackSubmitForm', $data );
	}
	// ------------------------------------------------------------------------
	/**
	 * 闪出数据
	 * 说明：通过session临时存储数据，并在下次读取后删除
	 */
	public function flash() {}
}